09. CODE: Add New Neighbors
Add New Neighbors
Before you will be able to finish writing the AStarSearch
method, you will need a way to add new nodes to the search.In this exercise, you will will write a RoutePlanner::AddNeighbors
method for this purpose.
This method will take each neighbor of the current node in the A* search, set the neighbor's g-value, h-value, and parent, and add the neighbor to the open list. To do this, you will use the RouteModel::Node::FindNeighbors()
, and the CalculateHValue
method that you have written previously.
## To complete this exercise:
- Modify
route_planner.h
to include a private method declaration for theAddNeighbors
method. This method should accept a pointer to aRouteModel::Node
object as the argument, and since the method is just modifying theRoutePlanner
instance variableopen_list
, the method can havevoid
return type. - In
route_planner.cpp
define theAddNeighbors
method using theFindNeighbors
andCalculateHValue
methods. Use the pseudocode below as a guideline:
## Pseudocode:
AddNeighbors(RouteModel::Node *current_node):
- Call
FindNeighbors()
oncurrent_node
to populate thecurrent_node
'sneighbors
vector. - For each
neighbor
in thecurrent_node
'sneighbors
- Set the
neighbor
sparent
to thecurrent_node
.
- Set the
neighbor
'sg_value
to the sum of thecurrent_node
'sg_value
plus the distance from thecurent_node
to theneighbor
.- Set the
neighbor
'sh_value
usingCalculateHValue
- Push the
neighbor
to the back of theopen_list
.- Mark the
neighbor
as visited.
Note: This exercise does not have any tests associated with it, but you will be completing A* search in the next exercise, and the code will be tested with the results of AStarSearch()
.
Workspace
This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity, so you may be able to download them there.
Workspace Information:
- Default file path:
- Workspace type: react
- Opened files (when workspace is loaded): n/a
-
userCode:
export CXX=g++-7
export CXXFLAGS=-std=c++17
cmake_tests() {
/usr/local/bin/cmake -DTESTING="AStarStub" "$1"
}
export -f cmake_tests
Solution
Add Neighbors